﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRJoint
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

internal class CApExportToRTRJoint : ICApExportToRTRWriteable, IJoint
{
  protected TxJoint m_joint;

  public CApExportToRTRJointData Data { get; set; }

  public CApExportToRTRJoint(CApExportToRTRJointData data) => this.Data = data;

  public CApExportToRTRJoint(TxJoint joint) => this.m_joint = joint;

  public virtual string GetJointName() => this.m_joint.Name;

  public virtual string GetParentLinkName() => this.m_joint.ParentLink.Name;

  public virtual string GetChildLinkName() => this.m_joint.ChildLink.Name;

  public virtual TxJoint GetParentJoint() => this.m_joint.ParentLink.ParentJoint;

  public void Write(CApExportToRTRXmlWriter writer)
  {
    writer.WriteStartElement("joint");
    writer.WriteAttributeString("name", this.Data.Name);
    writer.WriteAttributeString("type", this.Data.TypeString);
    if (this.Data.Index != -1)
      this.WriteIndex(writer);
    this.Data.Origin?.Write(writer);
    this.WriteLink(writer, "parent", this.Data.Parent);
    this.WriteLink(writer, "child", this.Data.Child);
    this.WriteAxis(writer);
    this.Data.Calibration?.Write(writer);
    this.Data.Dynamics?.Write(writer);
    this.Data.Limit?.Write(writer);
    this.Data.Mimic?.Write(writer);
    this.Data.SafetyController?.Write(writer);
    writer.WriteEndElement();
  }

  protected virtual void WriteIndex(CApExportToRTRXmlWriter writer)
  {
    writer.WriteAttributeString("index", this.Data.Index.ToString());
  }

  private void WriteLink(CApExportToRTRXmlWriter writer, string type, string name)
  {
    writer.WriteStartElement(type);
    writer.WriteAttributeString("link", name);
    writer.WriteEndElement();
  }

  private void WriteAxis(CApExportToRTRXmlWriter writer)
  {
    if (this.Data == null || this.Data.Axis == null || this.Data.Axis[0] == 0.0 && this.Data.Axis[1] == 0.0 && this.Data.Axis[2] == 0.0)
      return;
    writer.WriteStartElement("axis");
    writer.WriteAttributeString("xyz", ((IEnumerable<double>) this.Data.Axis).DoubleArrayToString());
    writer.WriteEndElement();
  }
}
